package contest.oop.sse.secalg;

/**
 * 凯撒算法
 *
 * @author paule
 */
public class CaesarAlgorithm extends SymmetricAlgorithm {
    public CaesarAlgorithm() {

    }

    public CaesarAlgorithm(String key) {
        this.key = key;
    }

    @Override
    public byte[] encrypt(String original) {
        String originalStr = original.toString();
        int offset = Integer.parseInt(this.key);
        byte[] result = new byte[originalStr.length()];
        for (int i = 0; i < originalStr.length(); i++) {
            result[i] = (byte) (originalStr.charAt(i) + offset);
        }
        return result;
    }

    @Override
    public String decrypt(byte[] cipher) {
        int offset = Integer.parseInt(this.key);
        StringBuilder builder = new StringBuilder();
        for (byte b : cipher) {
            builder.append((char) (b - offset));
        }
        return builder.toString();
    }


}
